library(estimatr) 
library(ggplot2)
library(dplyr)
library(dotwhisker)
library(gridExtra)
library(coefplot)
library(tidyverse)


# Figure 5: Probability of Selecting a Nuclear Response to a DPRK Attack


setwd("/Users/dmin/Dropbox/Replication_Data/Paper_1/")

COMdat <- read.csv("Paper_1_Data_Clean_tidy.csv", stringsAsFactors = FALSE)


gg_df <-
  COMdat %>%
  group_by(Nationality) %>%
  summarize(tidy(
    lm_robust(
      NuclearMissile ~ Japan + Nuclear + Tripwire + Retaliation,
      data = cur_data()
    )
  ))

gg_df <-
  gg_df %>%
  filter(term != "(Intercept)") %>%
  mutate(term_label =
           factor(
             term,
             levels = c("Nuclear",
                        "Tripwire",
                        "Retaliation",
                        "Japan"),
             labels = c(
               "DPRK Attack Uses\nNuclear Weapons",
               "US Military Casualties",
               "DPRK Threat of Retaliation",
               "Target is Japan"
             )
           ),
         
         nationality_label = factor(Nationality, levels = c(2, 1, 3),
                                    labels = c("Japan", "USA", "  S. Korea"))
  )



label_df <- 
  gg_df %>% 
  filter(term_label == "DPRK Attack Uses\nNuclear Weapons")


g <-
  ggplot(data = gg_df,
         aes(estimate, term_label, color = nationality_label, group = nationality_label, shape = nationality_label)) +
  geom_point(position = position_dodgev(height = 0.5)) +
  geom_linerange(aes(xmin = conf.low, xmax = conf.high), position = position_dodgev(height = 0.5)) +
  geom_text(data = label_df, aes(label = nationality_label, x = conf.high + 0.02), position = position_dodgev(height = 0.5)) +
  geom_vline(xintercept=0, linetype="dotted")+
  theme_bw() +
  theme(
    plot.title = element_text(face = "bold", size = 12),
    legend.position = "nonee",
    # legend.background = element_rect(colour="grey80"),
    axis.title.y = element_blank()
  ) +
  scale_color_manual(values = c( "indianred2","forestgreen","steelblue2")) +
  labs(x = "Average Treatment Effect Estimate") +
  scale_y_discrete(limits=rev)+
  coord_cartesian(xlim=c(-0.15,.18))
g

ggsave("Image_5.png", plot=g, width=2000, height=1200, units=c("px"))

